import axios from '../utils/axios'
import {userinfo} from "../utils/getUserInfo"

export const getCartList = async ()=>{
    const {uid} = userinfo();
    if(!uid) return false;
    const res = await axios.get("/cartlist",{params:{uid}});
    const list = []
    if(res.code === 200){
        res.list.forEach(item=>{
            if(!item.content) return false;
                item.content.forEach(obj=>{
                    obj.checked = true
                    list.push(obj)
                }
            )
        })
    }
    return list
}
export const cartAdd = (goodsid,type=1,num=1)=> {
    //type默认是普通商品，万人团type为2
    const {uid} = userinfo();
    return axios.post("/cartadd",{uid,goodsid,type,num})
}
export const cartDelete = (id)=> axios.post("/cartdelete",{id})
//计算购物车商品数量
export const getCartNumber = (cartList) =>{
    var number = 0
    cartList.forEach(item=>{
        number += item.num
    })
    return number
}